import axios from "axios"
// import { ElMessage } from 'element-plus'

// 创建 axios 的实例

const request = axios.create({
    baseURL: "http://localhost:3000/",
    timeout: 5 * 1000
});

// 请求拦截

request.interceptors.request.use(
    (config) => {

        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

// 响应拦截
request.interceptors.response.use(
    (response) => {
        const data = response.data
        // 注意js-server数据返回无code字段
        if (data.code && data.code != 200) {
            ElMessage({
                grouping: true,
                showClose: true,
                message: data.message,
                type: 'error',
            })
            throw new Error(data.message || "网络异常，请稍后重试！");
        }
        return data;
    },
    (error) => {
        ElMessage({
            grouping: true,
            showClose: true,
            message: error.message,
            type: 'error',
        })
        return Promise.reject(error);
    }
);

export default request;
